<?php
/* ========================================
 * --------------------
 *  InsertQuery.php
 * --------------------
 * Created on 30.03.2010
 * ========================================
 * $Revision: $
 * $Author: Dmitry Boycov
 * $HeadURL: $ 
 * $Id: $
 * ========================================
 * Changes ["date: description (who)"]:
 *
 * 30.03.2010: Original version (WHo)
 * 15:19:50: Time 
 */
require_once("include/bd/Query.php");

class InsertQuery extends Query{

	private $data = null;
	
	/**
	 * construct
	 * @param  $table
	 * @param  $data
	 * @return void
	 */
	public function __construct($table,$data=null){
		parent::__construct($table);
		$this->data = $data;
	}

	public function getData(){
		return $this->data;
	}

	public function setData($data){
		$this->data = $data;
	}

	/**
	 * generate insert statement:
	 *  INSERT INTO table_name (column1, column2, column3,...)
	 *  VALUES (value1, value2, value3,...)
	 * @return string
	 */
	public function execute(){
		if(is_null($this->data) || empty($this->data)) return '';
		if(is_null($this->table) || $this->table=='') return '';
		$v=''; $n='';
		$q="INSERT INTO `".$this->table."` ";
		foreach($this->data as $key=>$val) {
			$n.="`$key`, ";
			if(strtolower($val)=='null') $v.="NULL, ";
			elseif(strtolower($val)=='now()') $v.="NOW(), ";
			else $v.= "'".$this->escape($val)."', ";
		}
		$q .= "(". rtrim($n, ', ') .") VALUES (". rtrim($v, ', ') .");";        
		return $q;
	}

}
?>